package com.mall.manager.user.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.WebUtils;

import com.mall.manager.user.service.ManageUserService;
import com.mall.untils.MD5Util;
import com.mall.untils.Page;


/**
 * 
 * @author LISIQIANG
 * 处理用户增、删、改、查控制层
 */
@Controller
public class ManageUserController {
	Logger logger = LoggerFactory.getLogger(ManageUserController.class);
	@Resource ManageUserService userService;
	
	
	/**
     * 添加用户的转向链接
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/manager_user_insert")
    public String manager_user_insert(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	return "user/manager_user_add";
    }
    

	/**
	 * 添加某个用户
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
    @RequestMapping(value = "/manager_user_add")    
    public String manager_user_add(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {   
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	int update=userService.manager_user_add(paraMap);
    	 
    	if(update>0){   
    		modelMap.addAttribute("info", "添加成功！");
    		 return "redirect:/manager_user_list "; 
    	}else if(update==-1){
    		return "redirect:/manager_user_insert ";
    	}
    	
    	modelMap.addAttribute("info", "添加失败！");
		
    	 return "redirect:/manager_user_list "; 
    }
    
    
    /**
     * 修改某个用户
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    
    
    @RequestMapping(value = "/manager_user_update")    
    public String manager_user_update(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {   
    	    	
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	Map<String, Object> user = userService.manager_user_updateone(paraMap);	
    	user.put("pwd",MD5Util.convertMD5(String.valueOf(user.get("pwd"))));
    	modelMap.addAttribute("user", user);
    	return "user/manager_user_edit";
    	
    }
    
    
    /**
     * 修改某个用户
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    
    
    @RequestMapping(value = "/manager_user_edit")    
    public String manager_user_edit(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {   
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	int update=userService.manager_user_edit(paraMap);
    	
    	if(update>0){
    		modelMap.addAttribute("info", "修改成功！");
    		return "redirect:/manager_user_list?type=1";
    	}
    	modelMap.addAttribute("info", "修改失败！");
		return "redirect:/manager_user_list ";
       
    }
    
    /**
     * 删除某个用户
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    
    @RequestMapping(value = "/manager_user_delete")    
    public String manager_user_delete(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {   
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	int update=userService.manager_user_delete(paraMap);
    	if(update>0){
    		modelMap.addAttribute("info", "删除成功！");
    		return "redirect:/manager_user_list?type=1";
    	}
    	
    	modelMap.addAttribute("info", "删除失败！");
		return "redirect:/manager_user_list ";
    }
    
    /**
     * 分页查询用户信息
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @SuppressWarnings("unchecked")
	@RequestMapping(value = "/manager_user_list")    
    public String manager_user_list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {  
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	int type = MapUtils.getIntValue(paraMap, "type");
    	Page page= userService.manager_user_list(request);    	
     	if(null!=page){
     		
			List<Map<String,Object>> list = (List<Map<String,Object>>)page.getResult();
     		for(int i=0;i<list.size();i++){
     			list.get(i).put("pwd",MD5Util.convertMD5(String.valueOf(list.get(i).get("pwd"))));
     		}
	     	modelMap.addAttribute("manager_user_list", list);
	     	int totalpagecount = page.getTotalPageCount();
	     	modelMap.addAttribute("totalpagecount", totalpagecount);
	     	int currentpageno = page.getCurrentPageNo();
	     	modelMap.addAttribute("currentpageno", currentpageno);
	     	int previouspage = page.getCurrentPageNo() <= 1 ? 1 : page
					.getCurrentPageNo() - 1;  
	     	modelMap.addAttribute("previouspage", previouspage);
	     	int nextpage = page.getCurrentPageNo() >= page.getTotalPageCount() ? page
					.getTotalPageCount() : page.getCurrentPageNo() + 1;
			modelMap.addAttribute("nextpage", nextpage); 
			List<Integer> showNumList = page.getShowNumList();
			modelMap.addAttribute("pagenumlist", showNumList);
			modelMap.addAttribute("type", type);
     	}
         	     	
        return "user/manager_user_list";
    }
    
	
}
